package com.imm.da.main.weather.api

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.qweather.sdk.bean.base.Code
import com.qweather.sdk.bean.base.Lang
import com.qweather.sdk.bean.base.Unit
import com.qweather.sdk.bean.weather.WeatherNowBean
import com.qweather.sdk.view.QWeather

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2021/12/1
 */
class HeWeatherService constructor(private val context : Context){

    /**
     * 实时天气
     * @param location 所查询的地区，可通过该地区ID、经纬度进行查询经纬度格式：经度,纬度(英文,分隔，十进制格式，北纬东经为正，南纬西经为负)
     * @param lang (选填)多语言，可以不使用该参数，默认为简体中文
     * @param listener 网络访问结果回调
     */
    fun query(location : String,
              result : MutableLiveData<WeatherNowBean.NowBaseBean>,
              lang : Lang = Lang.ZH_HANS,
              unit : Unit = Unit.METRIC
        ){
        QWeather.getWeatherNow(context, location, lang, unit, object : QWeather.OnResultWeatherNowListener {
            override fun onSuccess(bean: WeatherNowBean?) {
                if (Code.OK == bean?.code){
                    result.value = bean.now
                }
            }
            override fun onError(t: Throwable?) {
                t?.printStackTrace()
            }
        })
    }

}